import { targetCuesActive, targetCuesTopic, targetPromotion, targetPublicity, trendRank } from "../../service/api/api-analysis";

// pages/analysis/index.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollTop: 0,//天数吸顶距离
    currentTab: 0,//当前选择的天数
    currentId: 1,//当前传入的type
    stickyTop: 18,//type选择距离顶部吸顶距离
    tabs: [{
      id: '1',
      name: "近7天"
    }, {
      id: '2',
      name: "近60天"
    }, {
      id: '3',
      name: "近90天"
    }],//天数选择
    currentDay: '-7',//当前选择的天数
    dropdownShow: false,//下拉选择框是否显示
    promotionData: {},//新媒连心桥数据
    topicData: {},//线索征集数据
    activeData: {},//主动报送数据
    publicityData: {},//宣传刊播数据
    rankList: [],//排行榜数据
    currentDayList: [],//当前时间区间
    selectTapName: '综合传播影响力',
    rankType: '0',//排行榜类型
    dropdownlistData: [{
      name: "综合传播影响力",
      type: '0'
    }, {
      name: "线索报送",
      type: '1'
    }, {
      name: "线索收录",
      type: '2'
    }, {
      name: "宣传刊播",
      type: '3'
    },
    //  {
    //   name: "新媒连心桥",
    //   type: '4'
    // }
    ],//排行榜下拉选择数据
  },
  //页面滚动执行方式
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop
    })
  },
  //获取当前时间区间
  getDayList(type: number) {
    console.log(type);
    
    const current = type === 0 ? 6 : type === 1 ? 59 : 89
    const currentDate = new Date();
    const currentTimestamp = currentDate.getTime();
    const ninetyDaysAgo = currentTimestamp - current * 24 * 60 * 60 * 1000;
    const startDateObj = new Date(ninetyDaysAgo);
    const endDateObj = new Date(currentTimestamp);
    const startDate = `${startDateObj.getFullYear()}-${startDateObj.getMonth() + 1}-${startDateObj.getDate()}`;
    const endDate = `${endDateObj.getFullYear()}-${endDateObj.getMonth() + 1}-${endDateObj.getDate()}`;
    this.setData({
      currentDayList: [startDate, endDate]
    })
  },

  //获取排行榜数据
  async getRankList(dates: number, type: any) {
    this.getDayList(dates)
    const res = await trendRank({ dates: this.data.currentDayList, type })
    this.setData({
      rankList: res.data
    })
    wx.hideLoading()
  },
  //调用此方法显示隐藏下拉列表
  dropDownList: function (e) {
    let index = Number(e.currentTarget.dataset.index)
    if (index !== -1) {
      this.setData({
        selectTapName: this.data.dropdownlistData[index].name,
        rankType: this.data.dropdownlistData[index]?.type
      })
      wx.showLoading({
        title: '加载中...'
      })
      if (this.data.dropdownlistData[index]?.type) {
        this.getRankList(this.data.currentId, this.data.dropdownlistData[index]?.type)
      }
    }
    this.setData({
      dropdownShow: !this.data.dropdownShow
    })
  },
  //天数选择
  change(e) {
    console.log(e);

    this.setData({
      currentTab: e.detail.index,
      currentId: e.detail.index + 1
    })
    wx.showLoading({
      title: '加载中...'
    })
    let currentDay = e.detail.index === 0 ? '-7' : e.detail.index === 1 ? '-60' : '-90'
    // this.getTargetPromotion(currentDay);
    this.getTargetCuesTopic(currentDay);
    this.getTargetCuesActive(currentDay);
    this.getTargetPublicity(currentDay);
    this.getRankList(e.detail.index, this.data.rankType);
  },
  //获取新媒连心桥数据
  async getTargetPromotion(currentDay: string) {
    const res = await targetPromotion({ day: currentDay })
    res.data.title = {
      text: '新媒连心桥',
      size: 34
    }
    res.data.header = {
      bgcolor: '#EFF9FE',
      line: true
    }
    const promotionData = res.data || {};
    this.setData({
      promotionData
    });
  },
  //获取线索征集数据 
  async getTargetCuesTopic(currentDay: string) {
    const res = await targetCuesTopic({ day: currentDay })
    const topicData = res.data || {};
    res.data.title = {
      text: '线索征集',
      size: 34
    }
    res.data.header = {
      bgcolor: '#F5F3FC',
      line: true
    }
    this.setData({
      topicData
    });
  },
  //获取主动报送数据 
  async getTargetCuesActive(currentDay: string) {
    const res = await targetCuesActive({ day: currentDay })
    const activeData = res.data || {};
    res.data.title = {
      text: '主动报送',
      size: 34
    }
    res.data.header = {
      bgcolor: '#FEF8F8',
      line: true
    }
    this.setData({
      activeData
    });
  },
  //获取宣传刊播数据
  async getTargetPublicity(currentDay: string) {
    const res = await targetPublicity({ day: currentDay })
    const publicityData = res.data || {};
    res.data.title = {
      text: '宣传刊播',
      size: 34
    }
    res.data.header = {
      bgcolor: '#FCFBF2',
      line: true
    }
    this.setData({
      publicityData
    });
    wx.hideLoading()
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    console.log('fewfe');
    
    wx.showLoading({
      title: '加载中...'
    })
    /**
     * 默认进页面
     */
    let currentDay = "-7"
    // this.getTargetPromotion(currentDay);
    this.getTargetCuesTopic(currentDay);
    this.getTargetCuesActive(currentDay);
    this.getTargetPublicity(currentDay);
    this.getRankList(0, 0);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

})